<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="Bookmark" href="/favicon.ico">
    <link rel="Shortcut Icon" href="/favicon.ico"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>
    <!--[if IE 6]>
    <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>药品列表</title>
</head>
<style>
    .table > tbody > tr > td {
        text-align: center;
    }
</style>
<body>

<div>
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 订单管理 <span
            class="c-gray en">&gt;</span> 订单列表 <a class="btn btn-success radius r"
                                                  style="line-height:1.6em;margin-top:3px"
                                                  href="javascript:location.replace(location.href);" title="刷新"><i
            class="Hui-iconfont">&#xe68f;</i></a></nav>
    <form class="page-container">
        <div class="cl pd-5 bg-1 bk-gray mt-20">

          <span class="l">
<%--            <a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i--%>
<%--                    class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>--%>
            <a href="javascript:;" onclick="medicineAdd('添加药品','medicine-add','','630')" class="btn btn-primary radius"><i
                    class="Hui-iconfont">&#xe600;</i> 添加</a>

        </span>
            <span class="l">
                <a class="btn btn-primary radius" onclick="downloadExcel()" href="javascript:;">导出excel</a>
            </span>
            <span class="l">
                <a class="btn btn-warning radius" onclick="uploadExcel()" href="javascript:;">导入excel</a>
            </span>


        </div>

        <div class="mt-20">
            <div class="mt-20" style="margin-bottom: 70px">
                <table class="table table-border table-bordered table-bg table-hover table-sort" width="100%">
                    <thead>
                    <tr class="text-c">
                        <th width="25"><input type="checkbox" name="" value=""></th>
                        <th width="80">药品ID</th>
                        <th width="80">药品名称</th>
                        <th width="80">商品名</th>
                        <th width="50">厂家</th>
                        <th width="50">规格</th>
                        <th width="60">单价</th>
                        <th width="80">是否冷藏</th>
                        <th width="50">状态</th>
                        <th width="50">操作</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </form>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="lib/datatables/dataTables.colReorder.min.js"></script>
<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>

<script type="text/javascript" src="lib/common.js"></script>
<script type="text/javascript">

    /*datatables配置*/
    var table = $('.table').DataTable({
        serverSide: true,//开启服务器模式
        "processing": true,//加载显示提示
        "ajax": {
            url: "./medicine/list",
            type: 'GET',
            data: {
                "searchKey": "",
                "minDate": "",
                "maxDate": "",
            }
        },
        "columns": [
            {
                "data": null,
                render: function (data, type, row, meta) {
                    return "<input name=\"checkbox\" value=\"" + row.orderId + "\" type=\"checkbox\" value=\"\">";
                }
            },
            {"data": "medicineId"},
            {"data": "medicineName"},
            {"data": "name"},
            {"data": "manufacturer"},
            {"data": "standard"},
            {"data": "price"},
            {"data": "freezing",
                render: function (data, type, row, meta) {
                    if (data == 1) {
                        return "是";
                    } else {
                        return "否";
                    }
                }
            },

            {
                "data": "state",
                render: function (data, type, row, meta) {
                    if (data == -1) {
                        return "<span class=\"label label-defant radius td-status\">已下架</span>";
                    } else if (data == 1) {
                        return "<span class=\"label label-success radius td-status\">已上架</span>";
                    } else {
                        return "<span class=\"label label-warning radius td-status\">其它态</span>";
                    }
                }
            },
            {
                "data": null,
                render: function (data, type, row, meta) {
                    if (row.state == 1) {
                        return "<a id=\"td-manage\" style=\"text-decoration:none\" onClick=\"stop(this," + row.medicineId + ")\" href=\"javascript:;\" title=\"下架\"><i class=\"Hui-iconfont\">&#xe631;</i></a> <a title=\"编辑\" href=\"javascript:;\" onclick=\"medicine_edit('编辑','medicine-edit'," + row.medicineId + ",'','510')\" class=\"ml-5\" style=\"text-decoration:none\"><i class=\"Hui-iconfont\">&#xe6df;</i></a> ";
                    } else {
                        return "<a id=\"td-manage\" style=\"text-decoration:none\" onClick=\"start(this," + row.medicineId + ")\" href=\"javascript:;\" title=\"上架\"><i class=\"Hui-iconfont\">&#xe6e1;</i></a> <a title=\"编辑\" href=\"javascript:;\" onclick=\"medicine_edit('编辑','medicine-edit'," + row.medicineId + ",'','510')\" class=\"ml-5\" style=\"text-decoration:none\"><i class=\"Hui-iconfont\">&#xe6df;</i></a>";

                    }
                }
            }
        ],
        "aaSorting": [[1, "desc"]],//默认第几个排序
        "bStateSave": false,//状态保存
        "aoColumnDefs": [
            {"orderable": false, "aTargets": [0]}// 制定列不参与排序
        ],
        language: {
            url: './lib/datatables/Chinese.json'
        },
        colReorder: true
    });

    var medicineId = "", medicineName = "", manufacturer = "", standard = "", state = "",freezing="",name="",price=0

    /*药品-编辑*/
    function medicine_edit(title, url, id, w, h) {
        medicineId = id;
        $('.table tbody').on('click', 'tr', function () {
            medicineName = table.row(this).data().medicineName;
            manufacturer = table.row(this).data().manufacturer;
            standard = table.row(this).data().standard;
            freezing = table.row(this).data().freezing;
            name = table.row(this).data().name;
            price = table.row(this).data().price;
        });
        layer_show(title, url, w, h);
    }

    /*药品-停用*/
    function stop(obj, id) {
        layer.confirm('确认要下架ID为\'' + id + '\'的药品吗？', {icon: 0}, function (index) {
            var index = layer.load(3);
            $.ajax({
                type: 'PUT',
                url: './medicine/stop/' + id,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.success != true) {
                        layer.alert(data.message, {title: '错误信息', icon: 2});
                        return;
                    }
                    refresh();
                    layer.msg('已停用!', {icon: 5, time: 1000});
                },
                error: function (XMLHttpRequest) {
                    layer.close(index);
                    layer.alert('数据处理失败! 错误码:' + XMLHttpRequest.status, {title: '错误信息', icon: 2});
                }
            });
        });
    }

    /*药品-启用*/
    function start(obj, id) {
        layer.confirm('确认要上架ID为\'' + id + '\'的药品吗？', {icon: 3}, function (index) {
            var index = layer.load(3);
            $.ajax({
                type: 'PUT',
                url: './medicine/start/' + id,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.success != true) {
                        layer.alert(data.message, {title: '错误信息', icon: 2});
                        return;
                    }
                    refresh();
                    layer.msg('已启用!', {icon: 6, time: 1000});
                },
                error: function (XMLHttpRequest) {
                    layer.close(index);
                    layer.alert('数据处理失败! 错误码:' + XMLHttpRequest.status, {title: '错误信息', icon: 2});
                }
            });
        });
    }


    /*订单-删除*/
    function order_del(obj, id) {
        layer.confirm('确认要删除ID为\'' + id + '\'的药品吗？', {icon: 0}, function (index) {
            var index = layer.load(3);
            $.ajax({
                type: 'DELETE',
                url: './medicine/del/' + id,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.success != true) {
                        layer.alert(data.message, {title: '错误信息', icon: 2});
                        return;
                    }
                    refresh();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                },
                error: function (XMLHttpRequest) {
                    layer.close(index);
                    layer.alert('数据处理失败! 错误码:' + XMLHttpRequest.status, {title: '错误信息', icon: 2});
                }
            });
        });
    }

    /*批量删除*/
    function datadel() {
        var cks = document.getElementsByName("checkbox");
        var count = 0, ids = "";
        for (var i = 0; i < cks.length; i++) {
            if (cks[i].checked) {
                count++;
                ids += cks[i].value + ",";
            }
        }
        if (count == 0) {
            layer.msg('您还未勾选任何数据!', {icon: 5, time: 3000});
            return;
        }
        /*去除末尾逗号*/
        if (ids.length > 0) {
            ids = ids.substring(0, ids.length - 1);
        }
        layer.confirm('确认要删除所选的' + count + '条数据吗？', {icon: 0}, function (index) {
            var index = layer.load(3);
            $.ajax({
                type: 'DELETE',
                url: './medicine/del/' + ids,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.success != true) {
                        layer.alert(data.message, {title: '错误信息', icon: 2});
                        return;
                    }
                    layer.msg('已删除!', {icon: 1, time: 1000});
                    refresh();
                },
                error: function (XMLHttpRequest) {
                    layer.close(index);
                    layer.alert('数据处理失败! 错误码:' + XMLHttpRequest.status, {title: '错误信息', icon: 2});
                }
            });
        });
    }

    function medicineAdd() {
        layer_show('添加药品', 'medicine-add')
    }

    function msgSuccess(content) {
        layer.msg(content, {icon: 1, time: 3000});
    }
    function downloadExcel(){
        $.ajax({
            type: 'get',
            url: './medicine/download',
            headers:{
                'Accept':'application/json, text/plain, */*',
                'Connection':'keep-alive' ,
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                'X-Requested-With': 'XMLHttpRequest'
            },
            xhrFields: { responseType: "arraybuffer" },
            success:function(data){
                var url = window.URL.createObjectURL(new Blob([data]))
                var a = document.createElement('a');
                a.style.display = 'none'
                a.href = url
                a.setAttribute('download', '药品列表.xlsx')
                a.click()
            },
            error:function(e){
                console.log(XMLHttpRequest)
                layer.alert('下载失败!',{title: '错误信息',icon: 2});
            }
        });
    }

    function uploadExcel(){
        layer_show('上传文件', 'choose-file')
    }
</script>
</body>
</html>